package com.wbxqy.car_management_system.config;


import com.wbxqy.car_management_system.convert.String2DateConvert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Slf4j
@Configuration
public class WebMVCConfig implements WebMvcConfigurer{

    /**
     * 跨域配置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        log.info("跨域配置");
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }

    /**
     * 拦截器配置
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        log.info("拦截器");
        registry.addConverter(new String2DateConvert());
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将本地目录D:/img映射到/img/**路径下
        registry.addResourceHandler("/img/**").addResourceLocations("file:D:/img/");
    }
}


